package com.ums.ribbonconsumer;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.ums.ribbonconsumer.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ConsumerController {

    /*-----------------从配置中心读取-------------------*/
    @Value("${ribbon-consumer.db_user}")
    private String db_user;

    @Value("${ribbon-consumer.db_password}")
    private String db_password;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    private HelloService helloService;

    @GetMapping(value = "/helloForFeign")
    public String helloForFeign(){
        return helloService.hello();
    }

    @PostMapping(value = "/placeOrderForFeign")
    public String placeOrderForFeign(@RequestBody() String body){
        return helloService.placeOrder(body);
    }

    public String helloError(){
        return "出错了";
    }

    @HystrixCommand(fallbackMethod = "helloError")
    @RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
    public String hello(){
        System.out.println("ConsumerController.hello");
        String url = "http://hello-service/hello";
        return restTemplate.getForEntity(url,String.class).getBody();   //通过restTemplate调用
    }

    @PostMapping(value = "/placeOrder")
    public Object queryOrder(@RequestBody String body){
        System.out.println("ConsumerController.queryOrder");
        if(body==null || body.length()<=0){
            return "请填写body";
        }
        return restTemplate.postForEntity("http://hello-service/placeOrder", body, Map.class).getBody();
    }

    @GetMapping("/get_db_info")
    public Map get_db_info(){
        System.out.println("ConsumerController.get_db_info");
        Map map = new HashMap();
        map.put("db_user",db_user);
        map.put("db_password", db_password);
        return map;
    }

    @GetMapping("/get_session_with_loadblance")
    public Map get_session_with_loadblance(HttpServletRequest request){
        request.getSession().setAttribute("user_name","loadblance");
        System.out.println("request.getSession().getId() = " + request.getSession().getId());
        ServiceInstance serviceInstance = loadBalancerClient.choose("hello-service");
        System.out.println(serviceInstance.getUri());
        Map map = new HashMap();
        map.put("date", new Date());
        return map;
    }

    @GetMapping("/get_session")
    public Map get_session(HttpServletRequest request){
        request.getSession().setAttribute("user_name","leishen");
        System.out.println("request.getSession().getId() = " + request.getSession().getId());

        Enumeration<String> headerNames = request.getHeaderNames();
        HttpHeaders requestHeaders = new HttpHeaders();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            String value = request.getHeader(key);
            requestHeaders.add(key, value);
        }

        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> response = restTemplate.exchange("http://hello-service/get_session",
                HttpMethod.GET, requestEntity, String.class);
        String sttr = response.getBody();
        Map map = new HashMap();
        map.put("date", new Date());
        map.put("session id",sttr);
        return map;
    }

    @GetMapping("/get_session_with_feign")
    public Map get_session_feign(HttpServletRequest request){
        request.getSession().setAttribute("user_name","zhangsan");
        //System.out.println(request.getSession().getAttribute("user_name"));
        System.out.println("request.getSession().getId() = " + request.getSession().getId());
        return helloService.getSession();
    }
}
